﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class PartStateBehaviourParameter:MonoBehaviour
{
    public InputField m_PartTypeDD;
    public Dropdown m_ParameterTypeDD;

    public GameObject m_FloatParameter;
    public GameObject m_BoolParameter;

    //  float
    public Slider m_ValueSlider;
    public Text m_ValueViewer;

    //  bool
    public Toggle m_ValueToggle;

    public ConditionList m_ConditionList;
    public Part m_Part;

    public ShipEventBehaviours m_Behavoir;
    public CallbackShipEventBehaviour onBehaviourChanged;
    private void Awake()
    {
        m_ValueViewer.text = "0";
    }
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    public void Init(ShipEventBehaviours behaviour)
    {
        if (behaviour == null)
            behaviour = new ShipEventBehaviours();
        var partStateBehaviour = behaviour as PartStateBehaviour;
        if (partStateBehaviour == null)
            return;
        m_Behavoir = partStateBehaviour;
        var part = m_ConditionList.m_Ship.FindPart(partStateBehaviour.m_PartID);
        SetPart(part);
        if (partStateBehaviour != null)
        {
            var behaviourMate = partStateBehaviour.behaviour;
            m_ParameterTypeDD.value = behaviourMate.m_ID;
            m_ValueSlider.value = (int)(behaviourMate.m_TargetData.m_TargetDataFloat * 100f);
            OnPartPartParameterTypeChanged(m_ParameterTypeDD.value);
        }
    }
    void SetPart(Part part)
    {
        if (part == null)
            return;
        m_Part = part;
        m_PartTypeDD.text = part.customName;

        if (m_Behavoir == null)
        {
            m_Behavoir = new PartStateBehaviour();
            if(onBehaviourChanged != null)
                onBehaviourChanged(m_Behavoir);
        }
        var partStateBehaviour = m_Behavoir as PartStateBehaviour;
        partStateBehaviour.m_PartID = part.m_ID;

        m_ParameterTypeDD.ClearOptions();
        var parameterList = part.GetParameterList();
        m_ParameterTypeDD.AddOptions(parameterList);
    }
    public void OnPartDropdownDrop(BaseEventData eventData)
    {
        var gameObject = eventData.selectedObject;
        if (!gameObject)
            return;
        var itemDrager = gameObject.GetComponent<ItemDrager>();
        if (!itemDrager)
            return;
        var part = itemDrager.m_Item as Part;
        if (part != null)
            m_Behavoir = null;
        SetPart(part);
    }
    public void OnPartPartParameterTypeChanged(int value)
    {
        if (m_Part == null)
            return;
        if(m_Behavoir.behaviour == null || (value > 0 && value != m_Behavoir.behaviour.m_ID))
        {
            m_Behavoir.behaviour = m_Part.GetDefaultInfoOfParameter(value);
        }
        var behaviourMate = m_Behavoir.behaviour;
        m_ValueSlider.maxValue = behaviourMate.m_MaxValue * 100f;
        m_ValueSlider.minValue = behaviourMate.m_MinValue * 100f;

        m_FloatParameter.gameObject.SetActive(false);
        m_BoolParameter.gameObject.SetActive(false);
        switch (m_Behavoir.behaviour.m_TargetData.m_DataType)
        {
            case CommonData.DataType.NONE:
                break;
            case CommonData.DataType.Float:
                m_FloatParameter.gameObject.SetActive(true);
                break;
            case CommonData.DataType.Bool:
                m_BoolParameter.gameObject.SetActive(true);
                break;
        }

    }
    public void OnFloatValueChanged()
    {
        var partBehaviour = m_Behavoir as PartStateBehaviour;
        if (partBehaviour == null || partBehaviour.m_Behaviour == null)
            return;
        float realValue = m_ValueSlider.value / 100f;
        partBehaviour.m_Behaviour.m_TargetData.m_TargetDataFloat = realValue;
        m_ValueViewer.text = realValue.ToString("0.00");
    }
    public void OnBoolValueChanged()
    {

    }
}